﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using System.Xml;
using System.Data;
using System.Xml.Linq;
using System.Xml.XPath;

namespace SharedFleetAccess
{
    public class XMLReader
    {
        private XmlDocument xDoc;
        private String route; 
        public Dictionary<String, _Vehicle> Info {get; set;} 

        public XMLReader()
        {
            Info = new Dictionary<string, _Vehicle>();
            route = @"C:\Users\ACER\Documents\Visual Studio 2010\Projects\CarDealer\SharedNFleet.xml";
            xDoc = new XmlDocument();
            xDoc.Load(route);

            // Iterate on the node set
            try
            {
                XmlNodeList vehicles = xDoc.GetElementsByTagName("fleet");
                XmlNodeList listaCars = ((XmlElement)vehicles[0]).GetElementsByTagName("Car");
                _Vehicle vnew = null;
                foreach (XmlElement nodo in listaCars)
                {
                    int i = 0;
                    XmlNodeList Reg_number = nodo.GetElementsByTagName("reg_number");
                    XmlNodeList Price = nodo.GetElementsByTagName("price");
                    XmlNodeList Colour = nodo.GetElementsByTagName("colour");
                    XmlNodeList State = nodo.GetElementsByTagName("state");
                    XmlNodeList Fuel = nodo.GetElementsByTagName("fuel");
                    XmlNodeList Type_car = nodo.GetElementsByTagName("type_car");
                    XmlNodeList Doors_number = nodo.GetElementsByTagName("doors_number");
                    vnew = new _Car(Reg_number[i].InnerText, Price[i].InnerText,
                        Colour[i].InnerText, State[i].InnerText, Fuel[i].InnerText,
                         Type_car[i].InnerText, Doors_number[i].InnerText);

                    Info.Add(Reg_number[i].InnerText, vnew);
                }
                XmlNodeList listaTrucks = ((XmlElement)vehicles[0]).GetElementsByTagName("Truck");
                foreach (XmlElement nodo in listaTrucks)
                {
                    int i = 0;
                    XmlNodeList Reg_number = nodo.GetElementsByTagName("reg_number");
                    XmlNodeList Price = nodo.GetElementsByTagName("price");
                    XmlNodeList Colour = nodo.GetElementsByTagName("colour");
                    XmlNodeList State = nodo.GetElementsByTagName("state");
                    XmlNodeList Fuel = nodo.GetElementsByTagName("fuel");
                    XmlNodeList High = nodo.GetElementsByTagName("high");
                    XmlNodeList Large = nodo.GetElementsByTagName("large");
                    XmlNodeList Width = nodo.GetElementsByTagName("width");
                    XmlNodeList Tonnage = nodo.GetElementsByTagName("tonnage");
                    XmlNodeList Num_Wheels = nodo.GetElementsByTagName("num_wheels");

                    vnew = new _Truck(Reg_number[i].InnerText, Price[i].InnerText,
                        Colour[i].InnerText, State[i].InnerText, Fuel[i].InnerText,
                         High[i].InnerText, Large[i].InnerText, Width[i].InnerText,
                         Tonnage[i].InnerText, Num_Wheels[i].InnerText);

                    Info.Add(Reg_number[i].InnerText, vnew);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


        }
        public Boolean reserve(String vehicleId)
        {
           // Create the filestream for saving
            FileStream WRITER = new FileStream(route, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
            int i=0;
            Boolean founded = false;
            Boolean available = false;
            XmlNodeList fleet = xDoc.GetElementsByTagName("fleet");
            XmlNodeList listaCars = ((XmlElement)fleet[0]).GetElementsByTagName("Car");
            foreach (XmlElement e in listaCars)
            {
                XmlNodeList Reg_number = e.GetElementsByTagName("reg_number");
                XmlNodeList State = e.GetElementsByTagName("state");
                if (Reg_number[i].InnerText.Equals(vehicleId))
                {
                    founded = true;
                    if (State[i].InnerText.Equals("Available"))
                    {
                        available = true;
                        State[i].InnerText = "Sold";
                    }
                }
            }
            if (!founded)
            {
                XmlNodeList listaTrucks = ((XmlElement)fleet[0]).GetElementsByTagName("Truck");
                foreach (XmlElement e in listaCars)
                {
                    XmlNodeList Reg_number = e.GetElementsByTagName("reg_number");
                    XmlNodeList State = e.GetElementsByTagName("state");
                    if (Reg_number[i].InnerText.Equals(vehicleId))
                    {
                        founded = true;
                        if (State[i].InnerText.Equals("Available"))
                        {
                            available = true;
                            State[i].InnerText = "Leased";
                        }
                    }
                }
            }
            // Save the xmldocument     
            xDoc.Save(WRITER);
            //Close the writer filestream
            WRITER.Close();

            return (founded && available);
        }
    }
    public class _Vehicle 
    {
        public String Reg_number { get; set; }
        public String Price { get; set; }
        public String Colour { get; set; }
        public String State { get; set; }
        public String Fuel { get; set; } 

        public _Vehicle(String rn, String p, String c, String st, String f)
        {
            Reg_number = rn;
            Price = p;
            Colour = c;
            State = st;
            Fuel = f;
        }
        
    }
    public class _Car : _Vehicle
    {
        public String Type_car { get; set; }
        public String Doors_number { get; set; }

       public _Car (String rn, String p, String c, String st, String f,String type, String dn) : base(rn,p,c,st,f){
           Type_car = type;
           Doors_number = dn;
         }
    }
    public class _Truck : _Vehicle
    {
        public String High { get; set; }
        public String Large { get; set; }
        public String Width { get; set; }
        public String Tonnage { get; set; }
        public String Num_Wheels { get; set; }

        public _Truck (String rn, String p, String c,String st, String f,
            String h, String l, String w, String t, String nw) : base(rn,p,c,st,f){

                High = h;
                Large = l;
                Width = w;
                Tonnage = t;
                Num_Wheels = nw;
         }
    }
}
